#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 10

/* 交换器 */
int swapNum(int *val1, int *val2)
{
    int tempVal = *val1;
    *val1 = *val2;
    *val2 = tempVal;
    return 0;
}


/* 升序 */
//数组做参数会自动退化为指针 
int bubbleSort01(int *nums, int numsSize)
{
    for(int jdx = numsSize; jdx > 0; jdx--)
    {
        /* 一轮 */

        for(int begin = 1; begin < jdx; begin++)
        {
            if(nums[begin-1] >nums[begin])
            {
                /* swap */
                swapNum(&(nums[begin-1]), &(nums[begin]));
            }
        }
    }
    return 0;
}

/* 升序 */
/* 
如果排好序了呢？
排好序就没必要再排序了
 */
//数组做参数会自动退化为指针 
int bubbleSort02(int *nums, int numsSize)
{
    int fiag = 0;//标签
    for(int jdx = numsSize; jdx > 0; jdx--)
    {
        /* 一轮 */
        fiag = 1;
        for(int begin = 1; begin < jdx; begin++)
        {
            if(nums[begin-1] >nums[begin])
            {
                fiag = 0;
                /* swap */
                swapNum(&(nums[begin-1]), &(nums[begin]));
            }
        }
        /* 已经排好序了 */
        if(fiag == 1)
        {
            break;
        }
    }
    return 0;
}



/* 打印 */
int printfNums(int *nums, int numsSize)
{
    for(int idx = 0; idx < numsSize; idx++)
    {
        printf("%d\t", nums[idx]);
    }
    printf("\n");
}


/* 找到数组的最小值 */
int findNumMinVal(int *nums, int numSize, int *pos)
{
    int adx = 0;
    for(int idx = 0; idx < numSize; idx++)
    {
        if(nums[idx] > nums[adx])
        {
            *pos = adx;
        }
        else
        {
            *pos = idx;
            adx = idx;  
        }
    }
    /* 返回最小值 */
    return nums[adx];
}


int main()
{
    int nums[] = {22, 1, 2, 66};
    /* 
    int nums[BUFFER_SIZE] = {22, 33, 44, 66};
    有了BUFFER_SIZE数组就硬性给你分配了100的空间，若没有BUFFER_SIZE，
    你有多少就分配多少，前提是你不超过BUFFER_SIZE
    */
    int numsSize = sizeof(nums) / sizeof(nums[0]);
    //int numsSize = strlen(nums);
    printf("numsSiz:%d\n", numsSize);

    int pos = 0;
    int minNum = findNumMinVal(nums, numsSize, &pos);
    bubbleSort01(nums, numsSize);
    bubbleSort02(nums, numsSize);
    //findNumMinVal(nums, numsSize, &pos);
    printf("pos:%d\n", pos);
    printf("minnum:%d\n", minNum);
    printfNums(nums, numsSize);
    

    return 0;
}

